
(function () {
    'use strict';

    CKEDITOR.plugins.add('uploadimage2', {
        lang: 'en,zh-cn',
        // jscs:enable maximumLineLength
        icons: 'uploadimage2', // %REMOVE_LINE_CORE%
        hidpi: false, // %REMOVE_LINE_CORE%
        init: function (editor) {

            let fileTools = CKEDITOR.fileTools,
                uploads = editor.uploadRepository;

            let supportedTypes = /image\/(jpeg|png|gif|bmp)/;
            let loadMethod = 'loadAndUpload';

            let tryDisplayHtml = function (totol, html, loader, state) {
                state.count++;
                if (state.noDisplay && state.count == totol) {
                    state.noDisplay = false;
                    loader.sender.editor.insertHtml(html);
                }
            };

            editor.addCommand('uploadimage2', {
                exec: function (editor) {
                    var $fileInput = document.createElement('input');
                    $fileInput.type = 'file';
                    $fileInput.multiple = true;
                    //$fileInput.accept = 'image/*';
                    $fileInput.click();
                    $fileInput.addEventListener('change', function (e) {
                        let fileList = [];
                        for (let i = 0; i < e.target.files.length; i++) {
                            fileList.push(e.target.files[i]);
                        }
                        e.target.value = null;

                        let uploadUrl = fileTools.getUploadUrl(editor.config, 'image');
                        let fieluploadUrl = fileTools.getUploadUrl(editor.config );

                        let html = '', state = { };
                        state.count = 0;
                        state.noDisplay = true;
                        
                        for (let i = 0; i < fileList.length; i++) {
                            let file = fileList[i];
                            let loader = uploads.create(file);

                            if (fileTools.isTypeSupported(file, supportedTypes)) {
                                loader[loadMethod](uploadUrl);

                                if (loadMethod == 'loadAndUpload' || loadMethod == 'upload') {
                                    CKEDITOR.fileTools.bindNotifications(editor, loader);
                                }

                                loader.on('uploaded', function (loader) {
                                    let newFileUrl = abp.remoteServiceFileBaseUrl + loader.sender.url;
                                    let img = new Image();
                                    img.src = newFileUrl;
                                    img.onload = function () {
                                        if (html) {
                                            html += '<br><img src="' + newFileUrl + '"' +
                                                ' width="' + img.width + '" ' +
                                                ' height="' + img.height + '">';
                                        } else {
                                            html += '<img src="' + newFileUrl + '"' +
                                                ' width="' + img.width + '" ' +
                                                ' height="' + img.height + '">';
                                        }

                                        tryDisplayHtml(fileList.length, html, loader, state);
                                    }
                                });

                                loader.on('error', function (loader) {
                                    tryDisplayHtml(fileList.length, html, loader, state);
                                    editor.showNotification(loader.message, 'warning');
                                });

                                loader.on('abort', function (loader) {
                                    tryDisplayHtml(fileList.length, html, loader, state);
                                    editor.showNotification(editor.lang.uploadwidget.abort, 'info');
                                });
                            }else{
                                // 附件上传
                                loader[loadMethod](fieluploadUrl);

                                if (loadMethod == 'loadAndUpload' || loadMethod == 'upload') {
                                    CKEDITOR.fileTools.bindNotifications(editor, loader);
                                }

                                loader.on('uploaded', function (loader) {
                                    if (html) {
                                        html += '<br>';
                                    } 

                                    let newFileUrl = abp.remoteServiceFileBaseUrl + loader.sender.url;

                                    var fileExName = file.name.substring(file.name.lastIndexOf(".") + 1).toLowerCase();
                                    if(fileExName === 'mp4'){
                                        html +="<div class=\"video-box\">";
                                        html +="	<video class=\"video-js vjs-default-skin vjs-big-play-centered\" controls=\"\" data-setup=\"{}\" playsinline=\"true\"  ";
                                        html +="	    style=\"width: 100%; height: 100%;\" webkit-playsinline=\"true\"  ";
                                        html +="	   x-webkit-airplay=\"allow\" x5-video-player-type=\"h5\">";
                                        html +="		<source src=\"" + newFileUrl +"\" type=\"video/mp4\" />";
                                        html +="	<p class=\"vjs-no-js\">To view this video please enable JavaScript, and consider upgrading to a web browser that<a href=\"http://videojs.com/html5-video-support/\" target=\"_blank\">supports HTML5 video</a></p>";
                                        html +="	</video>";
                                        html +="</div>";
                                    }else{
                                        html +='<a href="' + newFileUrl + '"  target="_blank" >'+ file.name +'</a>';
                                    }
                                    tryDisplayHtml(fileList.length, html, loader, state);
                                });

                                loader.on('error', function (loader) {
                                    tryDisplayHtml(fileList.length, html, loader, state);
                                    editor.showNotification(loader.message, 'warning');
                                });

                                loader.on('abort', function (loader) {
                                    tryDisplayHtml(fileList.length, html, loader, state);
                                    editor.showNotification(editor.lang.uploadwidget.abort, 'info');
                                });
                            }
                        }
                    })
                }
            });
            // Create and register toolbar button if possible.
            if (editor.ui.addButton) {
                editor.ui.addButton('UploadImage2', {
                    label: editor.lang.uploadimage2.labelName,
                    command: 'uploadimage2',
                    toolbar: 'insert,10',
                });
            }
        }
    });
})();

